//
//  AppDelegate.m
//  JiuwanBox
//
//  Created by lanou3g on 15/5/25.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "AppDelegate.h"
#import "JiongPictureViewController.h"
#import "RootViewController.h"
#import "ZWIntroductionViewController.h"
#import "LeftViewController.h"
#import "MMDrawerController.h"
#import "ShowVideoViewController.h"
#import "GestureRecognizerViewController.h"
#import "CLLockVC.h"
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
@interface AppDelegate ()

@property (nonatomic,strong) MMDrawerController * drawerController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor=[UIColor whiteColor];
    
    
   
    
    

    [self creatTabBarController];//创建tabBarControllerller
    
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]){
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
        [self creatIntroductionView];//创建引导页
        
        NSLog(@"第一次启动");
        
    }else{
        
        NSLog(@"不是第一次启动");
        
    }
    
//    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"firstStart"];
    
    
    
    [self creatDrawer];//创建抽屉
    
    
    [_window makeKeyAndVisible];
    
    
    BOOL hasPwd = [CLLockVC hasPwd];
    if(!hasPwd){
        
        NSLog(@"你还没有设置密码，请先设置密码");
    }else {
        
        [CLLockVC showVerifyLockVCInVC:_drawerController forgetPwdBlock:^{
            NSLog(@"忘记密码");
        } successBlock:^(CLLockVC *lockVC, NSString *pwd) {
            NSLog(@"密码正确");
            [lockVC dismiss:1.0f];
        }];
    }

    
    return YES;
}

//创建tabBarControllerller
-(void)creatTabBarController
{//吴
    ShowVideoViewController*showVC=[[ShowVideoViewController alloc]init];
    UINavigationController *showNC = [[UINavigationController alloc]initWithRootViewController:showVC];
    showVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"赛事直播" image:[UIImage imageNamed:@"video.png"] tag:102];
    
    JiongPictureViewController *JPVC=[[JiongPictureViewController alloc]initWithNibName:nil bundle:nil];
    JPVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"萌在靓图" image:[UIImage imageNamed:@"resize_png_new.png"] tag:101];
    UINavigationController *NCmain = [[UINavigationController alloc]initWithRootViewController:JPVC];
    NCmain.navigationBar.tintColor = [UIColor  colorWithRed:0.3 green:0.4 blue:0.5 alpha:1];
    
    RootViewController * rootVc = [[RootViewController alloc]init];
    rootVc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发现" image:[UIImage imageNamed:@"resize_png_new.png"] tag:102];
    UINavigationController * Nc = [[UINavigationController alloc]initWithRootViewController:rootVc];
    
    
    _TB = [[UITabBarController alloc]init];
    _TB.viewControllers = @[showNC,NCmain,Nc];
    _TB.tabBar.tintColor = [UIColor blackColor];
    _TB.tabBar.backgroundColor = [UIColor blackColor];
}


//创建引导页
-(void)creatIntroductionView
{
    NSArray * coverImageNames = @[@"img_index_01txt@2x.png",@"img_index_02txt@2x.png",@"img_index_03txt@2x.png"];
    NSArray * backgroundImageNames = @[@"1.jpg",@"2.jpg",@"3.jpg"];
    self.introductionView = [[ZWIntroductionViewController   alloc]initWithCoverImageNames:coverImageNames backgroundImageNames:backgroundImageNames];
    [_TB.view addSubview:self.introductionView.view];
    __weak AppDelegate *weakSelf = self;
    self.introductionView.didSelectedEnter = ^(){
        [weakSelf.introductionView.view removeFromSuperview];
        weakSelf.introductionView = nil;
    };
    [_window bringSubviewToFront:self.introductionView.view];

}


//创建抽屉
-(void)creatDrawer
{
    UIViewController * leftSideDrawerViewController = [[LeftViewController alloc] init];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yekong.png"]];
    imageView.frame = self.window.frame;
    [leftSideDrawerViewController.view addSubview:imageView];
    
    
    self.drawerController = [[MMDrawerController alloc]
                        initWithCenterViewController:_TB
                             leftDrawerViewController:leftSideDrawerViewController];
    
    [self.drawerController setShowsShadow:NO];
    [self.drawerController setRestorationIdentifier:@"MMDrawer"];
    [self.drawerController setMaximumLeftDrawerWidth:ScreenWidth-170];
    [self.drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [self.drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(20, CGRectGetHeight(self.window.frame)-80, 60, 15);
    [button setTitle:@"手势密码" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(gestureRecognizer) forControlEvents:(UIControlEventTouchUpInside)];
//    button.titleLabel.textColor = [UIColor whiteColor];
    button.tintColor = [UIColor blackColor];
    [leftSideDrawerViewController.view addSubview:button];
    
    
    
    [self.window setRootViewController:self.drawerController];
}


-(void)gestureRecognizer
{
    GestureRecognizerViewController *GRVC = [[GestureRecognizerViewController alloc]init];
    UINavigationController *NC = [[UINavigationController alloc]initWithRootViewController:GRVC];
    
    [self.drawerController presentViewController:NC animated:YES completion:nil];
}



- (UIViewController *)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
    NSString * key = [identifierComponents lastObject];
    if([key isEqualToString:@"MMDrawer"]){
        return self.window.rootViewController;
    }
    else if ([key isEqualToString:@"MMExampleCenterNavigationControllerRestorationKey"]) {
        return ((MMDrawerController *)self.window.rootViewController).centerViewController;
    }
    else if ([key isEqualToString:@"MMExampleRightNavigationControllerRestorationKey"]) {
        return ((MMDrawerController *)self.window.rootViewController).rightDrawerViewController;
    }
    else if ([key isEqualToString:@"MMExampleLeftNavigationControllerRestorationKey"]) {
        return ((MMDrawerController *)self.window.rootViewController).leftDrawerViewController;
    }
    else if ([key isEqualToString:@"MMExampleLeftSideDrawerController"]){
        UIViewController * leftVC = ((MMDrawerController *)self.window.rootViewController).leftDrawerViewController;
        if([leftVC isKindOfClass:[UINavigationController class]]){
            return [(UINavigationController*)leftVC topViewController];
        }
        else {
            return leftVC;
        }
        
    }
    else if ([key isEqualToString:@"MMExampleRightSideDrawerController"]){
        UIViewController * rightVC = ((MMDrawerController *)self.window.rootViewController).rightDrawerViewController;
        if([rightVC isKindOfClass:[UINavigationController class]]){
            return [(UINavigationController*)rightVC topViewController];
        }
        else {
            return rightVC;
        }
    }
    return nil;
}







- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
